package t2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t2bean.BookBean;
import t2bean.CardBean;
import t2bean.LibraryBean;

public class BookAction {

	private List<BookBean> list;
	private BookAction bookAction;
	private Scanner cin = Cin.open();

	public BookAction() {
	}

	// 进书
	public LibraryBean add(LibraryBean library) {

		// 输入书名
		System.out.println("输入书名：");
		String bookname = cin.nextLine();

		// 获取已经存储在library中的book数据
		BookBean book = new BookBean();
		list = new ArrayList<BookBean>();
		if (library.getBookList() != null) {
			list = library.getBookList();
		}

		// 将新数据写入list
		if (list != null) {
			book.setId(list.size() + 1);
			book.setName(bookname);
			book.setTimes(0);
			book.setLend(1);
			list.add(book);
		} else {
			book.setId(1);
			book.setName(bookname);
			book.setTimes(0);
			book.setLend(1);
			list.add(book);
		}

		// 将library中的数据替换为list
		library.setBookList(list);

		System.out.println("输入0返回,输入1继续进书");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return library;
		}
		if (str.equals("1")) {
			bookAction = new BookAction();
			bookAction.add(library);
		}
		return library;
	}

	// 查询某书借阅记录
	public void searchRecord(LibraryBean library) {

		// 输入数据
		System.out.println("输入查询的书名：");
		String bookname = cin.nextLine();

		// 获取存储在library中的数据
		List<BookRecordBean> list = new ArrayList<BookRecordBean>();
		list = library.getBookRecordList();

		// 查找输入的书名匹配的借阅记录
		for (int i = 0; i < list.size(); i++) {
			if (bookname.equals(list.get(i).getBook().getName())) {
				System.out.println(list.get(i).getInformation());
			}
		}

		System.out.println("输入0返回");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return;
		}
	}

	// 借书
	public LibraryBean lendBook(LibraryBean library) {

		// 输入数据
		System.out.println("输入要借的书：");
		String bookname = cin.nextLine();
		System.out.println("输入借书卡姓名：");
		String username = cin.nextLine();

		// 获取library中的数据
		List<BookBean> list = new ArrayList<BookBean>();
		list = library.getBookList();
		List<CardBean> cardlist = new ArrayList<CardBean>();
		cardlist = library.getCardList();
		List<CardRecord> cardRecordList = new ArrayList<CardRecord>();
		if (library.getCardRecordList() != null) {
			cardRecordList = library.getCardRecordList();
		}
		List<BookRecordBean> bookRecordList = new ArrayList<BookRecordBean>();
		if (library.getBookRecordList() != null) {
			bookRecordList = library.getBookRecordList();
		}

		// 根据书名查找书信息
		CardBean card = new CardBean();
		for (int i = 0; i < cardlist.size(); i++) {
			card = cardlist.get(i);
			if (username.equals(card.getName())) {
				break;
			}
		}

		// 查找输入的书名匹配的书信息

		for (int i = 0; i <= list.size(); i++) {
			BookBean book = list.get(i);
			if (bookname.equals(book.getName())) {
				// 判断书是否借出
				if (book.getLend() == 1) {

					CardRecord cardRecord = new CardRecord();
					if (cardRecordList != null) {
						cardRecord.setId(cardRecordList.size() + 1);
						cardRecord.setCard(card);
						cardRecord.setInformation("111");
					} else {
						cardRecord.setId(0);
						cardRecord.setCard(card);
						cardRecord.setInformation("111");
					}

					book.setLend(0);
					book.setTimes(book.getTimes() + 1);

					BookRecordBean bookRecord = new BookRecordBean();
					if (bookRecordList != null) {
						bookRecord.setId(bookRecordList.size() + 1);
						bookRecord.setBook(book);
						bookRecord.setInformation("111");
					} else {
						bookRecord.setId(1);
						bookRecord.setBook(book);
						bookRecord.setInformation("111");
					}

					cardRecordList.add(cardRecord);
					bookRecordList.add(bookRecord);
				} else {
					System.out.println("已借出");
				}
				break;
			}
		}

		// 将library中的数据替换为list
		library.setBookList(list);
		library.setBookRecordList(bookRecordList);
		library.setCardRecordList(cardRecordList);

		System.out.println("输入0返回,输入1继续借书");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return library;
		}
		if (str.equals("1")) {
			bookAction = new BookAction();
			bookAction.lendBook(library);
		}
		return library;
	}

	// 还书
	public LibraryBean returnBook(LibraryBean library) {

		// 输入数据
		System.out.println("输入要还的书：");
		String bookname = cin.nextLine();

		// 获取library中的数据
		List<BookBean> list = new ArrayList<BookBean>();
		list = library.getBookList();

		// 查找输入的书名匹配的书信息
		for (int i = 0; i <= list.size(); i++) {
			BookBean book = list.get(i);
			if (bookname.equals(book.getName())) {
				// 将状态改为未借出
				book.setLend(1);
				break;
			}
		}

		// 将library中的数据替换为list
		library.setBookList(list);

		System.out.println("输入0返回,输入1继续还书");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return library;
		}
		if (str.equals("1")) {
			bookAction = new BookAction();
			bookAction.returnBook(library);
		}
		return library;
	}

	public void showBook(LibraryBean library) {

		// 获取数据
		List<BookBean> list = new ArrayList<BookBean>();
		list = library.getBookList();

		// 按照借阅次数排序
		BookBean book = new BookBean();
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < list.size() - i - 1; j++) {
				BookBean book1 = list.get(j);
				BookBean book2 = list.get(j + 1);
				if (book1.getTimes() < book2.getTimes()) {
					book = book1;
					list.set(j, book2);
					list.set(j + 1, book);
				}
			}
		}

		// 输出
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getName() + " " + list.get(i).getTimes());
		}

		System.out.println("输入0返回");
		String str;
		str = cin.nextLine();
		if (str.equals("0")) {
			return;
		}
	}
}
